package financier.jfz.com.babycare;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jfz.quick.core.db.LightOrmHelper;

import java.util.ArrayList;
import java.util.List;

import financier.jfz.com.babycare.base.BaseActivity;
import financier.jfz.com.babycare.bean.BaseModle;

public class ModelListActivity extends BaseActivity {
    RecyclerView recyclerView;
    List<BaseModle> data = new ArrayList<>();
    Adater adater;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modellist_layout);
        initUI();
    }

    private void initUI() {
        getToolbar().setMainTitle("数据模型列表");
        recyclerView = findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adater = new Adater();
        recyclerView.setAdapter(adater);
        findViewById(R.id.tv_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View dialogView = LayoutInflater.from(view.getContext()).inflate(R.layout.title_dialog, null);
                final TextView textView = dialogView.findViewById(R.id.tv_name);
                new AlertDialog.Builder(view.getContext())
                        .setTitle("創建模型")
                        .setView(dialogView).setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (TextUtils.isEmpty(textView.getText())) {
                            return;
                        }
                        dialogInterface.dismiss();
                        startActivity(new Intent(ModelListActivity.this, AddMethodActivity.class).putExtra(AddMethodActivity.TitleName, textView.getText().toString()));
                    }
                }).create().show();

            }
        });
        getData();
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivityForResult(intent,1000);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        this.data.clear();
        getData();
    }

    private void getData() {
        List<BaseModle> baseModleList = LightOrmHelper.getInstent().query(BaseModle.class);
        data.addAll(baseModleList);
        adater.notifyDataSetChanged();
    }

    public class Adater extends RecyclerView.Adapter<ModelListActivity.Holder> {

        @NonNull
        @Override
        public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.model_item_layout, viewGroup, false);
            return new Holder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull Holder holder, int i) {
            holder.textView.setText(data.get(i).getName());
        }

        @Override
        public int getItemCount() {
            return data.size();
        }
    }

    public class Holder extends RecyclerView.ViewHolder {
        TextView textView;

        public Holder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv_name);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(new Intent(ModelListActivity.this, AddMethodActivity.class).putExtra(AddMethodActivity.MSG, data.get(getAdapterPosition())));
                }
            });
        }
    }

}
